package common

import (
	expect "github.com/google/goexpect"
	"regexp"
	"strings"
	"time"
)

//	func sendStorageMsg(e *expect.GExpect, cmd string) (string, error) {
//		fmt.Println("发送信息", cmd)
//		return sendMsg(e, cmd, storageReg)
//	}
func sendRootMsg(e *expect.GExpect, cmd string) (string, error) {
	return sendMsg(e, cmd, rootReg)
}
func sendRootMsgEuler(e *expect.GExpect, cmd string) (string, error) {
	return sendMsgEulerGetInfo(e, cmd, rootReg)
}
func sendUserMsg(e *expect.GExpect, cmd string) (string, error) {
	return sendMsg(e, cmd, userReg)
}
func sendPasswdMsg(e *expect.GExpect, cmd string) (string, error) {
	return sendMsg(e, cmd, passwdReg)
}

func sendMsgEulerGetInfo(e *expect.GExpect, cmd string, reg *regexp.Regexp) (string, error) {
	err := e.Send(cmd + "\n")
	if err != nil {
		return "", err
	}
	time.Sleep(1 * time.Second)
	s, _, err := e.Expect(reg, TIMEOUT)
	if err != nil {
		return "", err
	}
	//fmt.Println("______________s_______________")
	//log.Log("%s\n", s)
	//fmt.Println("______________s1_______________")
	//log.Log("%+v\n", s1)
	//fmt.Println("_____________________________")

	split := strings.Split(s, "\n")
	//log.Log("%+v", split)
	//log.Log(split[0])
	//log.Log(split[1])
	//log.Log("%s\n", split[1])

	//if len(split) < 2 {
	//	return "", nil
	//}
	msg := strings.Join(split[0:len(split)-1], "\n")
	//msg = strings.ReplaceAll(msg, "\033", "")
	//msg = strings.ReplaceAll(msg, "[?2004l", "")
	//msg = strings.ReplaceAll(msg, "[01;31m[K", "")
	//msg = strings.ReplaceAll(msg, "[m[K", "")
	//msg = strings.ReplaceAll(msg, "\r", "")
	//log.Log(mystrings.StrLog(msg))
	return StrLog(msg), nil
}
func sendMsgEuler(e *expect.GExpect, cmd string, reg *regexp.Regexp) (string, error) {
	err := e.Send(cmd + "\n")
	if err != nil {
		return "", err
	}
	time.Sleep(1 * time.Second)
	s, _, err := e.Expect(reg, TIMEOUT)
	if err != nil {
		return "", err
	}
	//fmt.Println("______________s_______________")
	//log.Log("%s\n", s)
	//fmt.Println("______________s1_______________")
	//log.Log("%+v\n", s1)
	//fmt.Println("_____________________________")

	split := strings.Split(s, "\n")
	//log.Log("%+v", split)
	//log.Log(split[0])
	//log.Log(split[1])
	//log.Log("%s\n", split[1])

	if len(split) < 2 {
		return "", nil
	}
	msg := split[1]
	//log.Log(mystrings.StrLog(msg))
	return msg, nil
}
func sendStorageMsg(e *expect.GExpect, cmd string) (string, error) {
	err := e.Send(cmd + "\n")
	if err != nil {
		return "", err
	}
	s, _, err := e.Expect(storageReg, TIMEOUT)
	if err != nil {
		return "", err
	}
	//split := strings.Split(s, "\n")
	//fmt.Println(split)
	//fmt.Println(s2)
	return s, nil
}
func sendMsg(e *expect.GExpect, cmd string, reg *regexp.Regexp) (string, error) {
	err := e.Send(cmd + "\n")
	if err != nil {
		return "", err
	}
	s, _, err := e.Expect(reg, TIMEOUT)
	if err != nil {
		return "", err
	}
	split := strings.Split(s, "\n")
	//fmt.Println("***************************************")
	//fmt.Println(split)
	//fmt.Println("***************************************")

	//log.Log("%+v", split)
	msg := split[0]
	return msg, nil
}
func sendH3cMsg(e *expect.GExpect, cmd string, reg *regexp.Regexp) (string, error) {
	err := e.Send(cmd + "\n")
	if err != nil {
		return "", err
	}
	s, _, err := e.Expect(reg, TIMEOUT)
	if err != nil {
		return "", err
	}

	return s, nil
}
